/**
 * 功能描述
 */

define(function (require, exports, module) {
	var Subnav = require("subnav");
	var Dialog = require("dialog");
	var user = require("store").get("user");

	
	var map = ["dayOne","dayTwo","dayThree","dayFour","dayFive","daySix","daySeven","dayEight","dayNine","dayTen","dayEleven","dayTwelve","dayThirdteen","dayFourteen","dayFifteen"];
	
    var instance = {
        initComponent: function (params,widget) {
        	var subnav = new Subnav({
        		parentNode: ".J-subnav",
        		model: {
        			title: "在住预约统计",
        			items: [{
        				id:"servicepoint",
            			type:"buttongroup",
            			items:user.privateServicePoints,
//            			url:"api/servicepoint/operableservicepoints",
            			tip:"服务点",
						keyField :"pkServicePoint",
						valueField :"name",
            			params:{
							fetchProperties:"pkServicePoint,name"
						},
						handler: function(key,element){
							widget.get("grid").refresh();
							
							widget.get("grid").refresh({
								"pkServicePoint": subnav.getValue("servicepoint"),
								"startDate": subnav.getValue("visitDate").start,
								"endDate": subnav.getValue("visitDate").end,
								fetchProperties:"roomtypename," 
        							+ "roomcount,"
        							+ "dayOne,"
        							+ "dayTwo,"
        							+ "dayThree,"
        							+ "dayFour,"
        							+ "dayFive,"
        							+ "daySix,"
        							+ "daySeven,"
        							+ "dayEight,"
        							+ "dayNine,"
        							+ "dayTen,"
        							+ "dayEleven,"
        							+ "dayTwelve,"
        							+ "dayThirdteen,"
        							+ "dayFourteen,"
        							+ "dayFifteen,"
							})
						}
        			},{
        				id:"visitDate",
						tip:"日期",
						type:"daterange",
						ranges:{
							"时间":[moment(),moment().add(14,"day")],
						},
						defaultRange : "时间",
						handler : function(time){
							 widget.get("grid").refresh();
							 
							var columns = [{
				    			name:"roomtypename",
								label:"房型",
				    		},{
				    			name:"roomcount",
								label:"总房数",
				    		}];
							
							var startDate = widget.get("subnav").getValue("visitDate").start;
				        	var endDate = widget.get("subnav").getValue("visitDate").end;
				        	
				        	var visitDays=((endDate+1)-startDate)/1000/60/60/24;
				        	console.log(visitDays);
				        	
				        	if(visitDays <= 15){
				        		for(var i=0;i < visitDays;i++){
				        			var select ;
				                	
				                	if(moment(startDate).add(i,"days").format("d")==0){
				                		select ="日"
				        			}else if(moment(startDate).add(i,"days").format("d")==1){
				        				select = "一"
				        			}else if(moment(startDate).add(i,"days").format("d")==2){
				        				select = "二"
				        			}else if(moment(startDate).add(i,"days").format("d")==3){
				        				select = "三"
				        			}else if(moment(startDate).add(i,"days").format("d")==4){
				        				select = "四"
				        			}else if(moment(startDate).add(i,"days").format("d")==5){
				        				select = "五"
				        			}else if(moment(startDate).add(i,"days").format("d")==6){
				        				select = "六"
				            	    }
				            		columns.push({
				            			name: map[i],
				            			label: moment(startDate).add(i,"days").format("MM.DD") + "/周" +select
				            		})
				            	}
				        	}else{
				        		for(var i=0;i < 15;i++){
                                    var select ;
				                	
				                	if(moment(startDate).add(i,"days").format("d")==0){
				                		select ="日"
				        			}else if(moment(startDate).add(i,"days").format("d")==1){
				        				select = "一"
				        			}else if(moment(startDate).add(i,"days").format("d")==2){
				        				select = "二"
				        			}else if(moment(startDate).add(i,"days").format("d")==3){
				        				select = "三"
				        			}else if(moment(startDate).add(i,"days").format("d")==4){
				        				select = "四"
				        			}else if(moment(startDate).add(i,"days").format("d")==5){
				        				select = "五"
				        			}else if(moment(startDate).add(i,"days").format("d")==6){
				        				select = "六"
				            	    }
				            		columns.push({
				            			name: map[i],
				            			label: moment(startDate).add(i,"days").format("MM.DD") + "/周" +select
				            		})
				            	}
				        		 widget.get("grid").refresh();
				        	}
				        	var grid = widget.get("grid");
				        	grid.get("model").columns = columns;  
				        	grid.renderPartial("thead");  
							 widget.get("grid").refresh({
								    "pkServicePoint": widget.get("subnav").getValue("servicepoint"),
								    "startDate": widget.get("subnav").getValue("visitDate").start,
									"endDate": widget.get("subnav").getValue("visitDate").end,
									fetchProperties:"roomtypename," 
	        							+ "roomcount,"
	        							+ "dayOne,"
	        							+ "dayTwo,"
	        							+ "dayThree,"
	        							+ "dayFour,"
	        							+ "dayFive,"
	        							+ "daySix,"
	        							+ "daySeven,"
	        							+ "dayEight,"
	        							+ "dayNine,"
	        							+ "dayTen,"
	        							+ "dayEleven,"
	        							+ "dayTwelve,"
	        							+ "dayThirdteen,"
	        							+ "dayFourteen,"
	        							+ "dayFifteen,"
							 });
						}
        			}]
        			
        		}
        	});
        	 widget.set("subnav",subnav);
        }
    };
    module.exports = instance;
});
